import api from "@/api/api";
import { CUSTOMER_INFO } from "./constant";
import commonSensors from '@/utils/commonSensors.js'

export const requestSubscribeMessageSync = (tmplIds) => {
  return new Promise((resolve) => {
    commonSensors.popupEvent('PopupExposure', {'popup_type' : '订阅消息', 'popup_name': '订阅消息弹窗'})
    commonSensors.modelAuthAsk('商品', tmplIds)
    uni.requestSubscribeMessage({
      tmplIds,
      success: (res) => {
        const r = [];
        for (const key in res) {
          if (res[key] === "accept") {
            r.push(key);
          }
        }
        commonSensors.popupEvent('PopupClick', {'popup_type' : '订阅消息', 'popup_name': '订阅消息弹窗', 'button_name': r.length > 0 ? '允许' : '拒绝'})
        // console.log("zlk requestSubscribeMessageSync success:", res);
        resolve(r);
      },
      fail: (err) => {
        // console.log("zlk requestSubscribeMessageSync fail:", err);
        resolve([]);
      },
    });
  });
};

// 需要根据环境做区分
const activitySubscribeId = api.environment === 'formal' ? 216 : 124;

export const getSubscribeMessageInfo = async (orderCode) => {
  try {
    const customerInfo = uni.getStorageSync(CUSTOMER_INFO) || "";
    const res = await api.getweixinSubscribe({
      method: "POST",
      header: {
        jwt: customerInfo.jwt,
      },
      query: {
        name: "活动模版消息",
        appId: getApp().globalData.app_id,
        openId: customerInfo.openId,
        params: {
          activitySubscribeId,
          referenceId: orderCode
        },
      },
    });
    // console.log("zlk getSubscribeMessageInfo res:", res);
    if (res.code === 1000) {
      return res.data;
    } else {
      return null;
    }
  } catch (error) {
    // console.error("zlk getSubscribeMessageInfo error:", error);
    return null;
  }
};

export const getWaitForPayTemplateId = async () => {
  try {
    const customerInfo = uni.getStorageSync(CUSTOMER_INFO) || "";
    const res = await api.queryActivitySubscribeDetail(
      {
        header: {
          jwt: customerInfo.jwt,
        },
      },
      activitySubscribeId
    );
    // console.log("zlk getWaitForPayTemplateId res:", res);
    if (res.code === 1000) {
      const { templateId } = res.data;
      return templateId;
    }
    return "";
  } catch (error) {
    // console.error("zlk getWaitForPayTemplateId error:", error);
    return "";
  }
};

export const sendWaitForPaySubscribeMessage = async (orderCode) => {
  try {
    if (!orderCode) {
      return
    }
    const customerInfo = uni.getStorageSync(CUSTOMER_INFO);
    const res = await api.weixinSubscribe({
      method: "POST",
      header: {
        jwt: customerInfo.jwt,
      },
      query: {
        name: "活动模版消息",
        type: "活动消息",
        appId: getApp().globalData.app_id,
        openId: customerInfo.openId,
        params: {
          activitySubscribeId,
          referenceId: orderCode
        },
      },
    });
    // console.log("zlk sendWaitForPaySubscribeMessage res:", res);
  } catch (error) {
    // console.error("zlk sendWaitForPaySubscribeMessage error:", error);
  }
};
